package com.raincat.toolsdemo.learn;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.raincat.tools.nettools.HttpsConfig;
import com.raincat.tools.nettools.NetCallBack;
import com.raincat.toolsdemo.R;
import com.raincat.toolsdemo.vaccin.HospitalListBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <pre>
 *     author : RainCat
 *     org    : Shenzhen JingYu Network Technology Co., Ltd.
 *     e-mail : nining377@gmail.com
 *     time   : 2020/08/16
 *     desc   : 学习强国查询
 *     version: 1.0
 * </pre>
 */
public class LearnStrongActivity extends AppCompatActivity {
    private Context context;
    private ListView lv_answer;
    private EditText et_query;

    private ListAdapter listAdapter;
    private HospitalListBean hospitalListBean;
    private Gson gson;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_learnstrong_main);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        context = LearnStrongActivity.this;
        gson = new Gson();

        init();
        listener();
    }

    private void init() {
        et_query = findViewById(R.id.et_query);
        lv_answer = findViewById(R.id.lv_answer);
    }

    private void listener() {
        findViewById(R.id.btn_query).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getAnswer();
            }
        });

        findViewById(R.id.btn_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                et_query.setText("");
            }
        });

        et_query.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    findViewById(R.id.btn_query).performClick();
                    return true;
                }
                return false;
            }
        });
    }

    private void getAnswer() {
        HashMap<String, Object> header = new HashMap<>();
        header.put("Host", "tzdt.qiuguofood.com");
        header.put("Referer", "https://servicewechat.com/wx578e60640fb57da3/4/page-frame.html");
        HashMap<String, Object> param = new HashMap<>();
        param.put("tKeyword", et_query.getText().toString());

        new HttpsConfig().doPost("https://tzdt.qiuguofood.com/topic-data/selectAll", param)
                .headers(header).start(context, new NetCallBack() {
            @Override
            public void result(String result) {
                LearnStrongBean learnStrongBean = gson.fromJson(result, LearnStrongBean.class);
                if (learnStrongBean != null && learnStrongBean.getData() != null)
                    listAdapter = new ListAdapter(learnStrongBean.getData());
                else
                    listAdapter = new ListAdapter(new ArrayList<LearnStrongBean.DataBean>());
                lv_answer.setAdapter(listAdapter);
            }

            @Override
            public void error(int errorCode, String error) {
                Toast.makeText(context, error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private class ListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        private List<LearnStrongBean.DataBean> mList;

        ListAdapter(List<LearnStrongBean.DataBean> l) {
            mInflater = LayoutInflater.from(context);
            mList = l;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ListAdapter.Holder holder;
            if (convertView == null) {
                holder = new ListAdapter.Holder();
                convertView = mInflater.inflate(R.layout.activity_hospitallist_item, parent, false);
                holder.text = convertView.findViewById(R.id.tv_item);
                convertView.setTag(holder);
            } else
                holder = (ListAdapter.Holder) convertView.getTag();

            LearnStrongBean.DataBean listBean = mList.get(position);
            holder.text.setText(changeTextColor(listBean.getTstem()));

            return convertView;
        }

        class Holder {
            TextView text;
        }
    }

    private SpannableStringBuilder changeTextColor(String text) {
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        int start = text.indexOf("【");
        int end;
        if (start != -1) {
            end = text.indexOf("】", start);
            ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
            builder.setSpan(redSpan, start, end + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return builder;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
